<?php include('../includes/header.php'); ?>
<?php include('../includes/mysqli_connect.php'); ?>
<?php include('../includes/sidebar-admin.php'); ?>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $errors = array();
    if (empty($_POST['category'])) {
        $errors[] = 'category';
    } else {
        $cat_name = mysqli_real_escape_string($dbc, strip_tags($_POST['category']));
    }
    if (isset($_POST['position']) && filter_var($_POST['position'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
        $position = $_POST['position'];
    } else {
        $errors[] = 'position';
    }
    if (empty($errors)) {
        $q = "INSERT INTO categories(user_id, cat_name, position) VALUES(1, '{$cat_name}',$position)";
        $r = mysqli_query($dbc, $q) or die("Query {$q} loi mat roi \n<br /> Mysql Error: " . mysqli_error($dbc));
        if (mysqli_affected_rows($dbc)) {
            $messages = '<p class="success">Them moi thanh cong</p>';
        } else {
            $messages = "<p class='warning'>Qua trinh them du lieu xay ra loi</p>";
        }
    } else {
        $messages = "<p class='warning'>dien day du thong tin vao</p>";
    }
}
?>
<div id="content">
    <h2>Create a category</h2>  
    <!--<p class="warning"></p>-->
    <!--<p class="success"></p>-->
    <?php
    if (!empty($messages)) {
        echo $messages;
    }
    ?>
    <form id="add_cat" name="add_cat" action="" method="post">
        <fieldset>
            <legend>Add category</legend>
            <div>
                <label for="category">Category Name: <span class="required">*</span>
                    <?php
                    if (isset($errors) && in_array('category', $errors)) {
                        echo '<p class="warning">Vui long dien thong tin category</p>';
                    }
                    ?>
                </label>
                <input type="text" name="category" id="category" value="<?php if (isset($_POST['category'])) {
                        echo strip_tags($_POST['category']);
                    } ?>" size="20" maxlength="150" tabindex="1" />
            </div>
            <div>
                <label for="position">Position: <span class="required">*</span>
                    <?php
                    if (isset($errors) && in_array('position', $errors)) {
                        echo "<p>Vui long dien thong tin position</p>";
                    }
                    ?>
                </label>
                <select name="position" tabindex='2'>
                    <?php
                    $q = "SELECT count(cat_id) AS COUNT from categories";
                    $r = mysqli_query($dbc, $q)or die("Query {$q} loi mat roi \n<br /> Mysql Error: " . mysqli_error($dbc));
                    if (mysqli_num_rows($r) == 1) {
                        list($num) = mysqli_fetch_array($r, MYSQLI_NUM);
                        for ($i = 1; $i <= $num + 1; $i++) {
                            echo "<option value='{$i}' ";
                            if(isset($_POST['position']) && $_POST['position']==$i){
                                echo "selected='selected'";
                            }
                            echo ">" . $i . "</option>";
                        }
                    }
                    ?>
                </select>
            </div>            
        </fieldset>
        <p><input type="submit" name="submit" value="Add Category" /></p>
    </form>
</div><!--end content-->
<?php // include('../include/sidebar-b.php');  ?>
<?php include('../includes/footer.php'); ?>